﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Controls;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Database;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SummaryForm.xaml
    /// </summary>
    public partial class EK03 : DocumentBase
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        INTEXDataSet iNTEXDataSet;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        //private Dictionary<string, object> dicQueryParamater;
        public DataSet dsResult;
        public DataSet setUI = new DataSet();
        EK03Data m_Data, m_DataX;
        EK03Report m_rpt;
        public DataSet dsParamaters;
        DataRow dwUI;
        DataTable tbUI;
        private string fromorderdate = "", endorderdate = ""
    , fromincomedate = "", endincomedate = "";
        private string m_GSART, m_GSVAR, m_GSCOLOR, m_Supplier;
        #endregion
        public EK03()
        {
            InitializeComponent();
            this.Name = "采购原料报表";
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        { 
            base.SetUp(dicParamater);
            InitializeComponent();
             
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            dsParamaters = (DataSet)dicParamater["dsParamaters"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            orlReadCommand = OracleDB.CreateCommand();
            orlReadCommand.Parameters.Add("OrdNum", OracleType.NVarChar).Value = "";
            m_Data = new EK03Data();
            m_Data.OracleDB = this.OracleDB;
            m_Data.proxy = this.proxy;
            m_rpt = new EK03Report(dicParamater);
            using (new LongOperation(this, "读取符合条件的记录"))
            {
                InitDataBinding();
            }
        }
        private void ItemsControls_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        } 
        private void InitDataBinding()
        {
            try
            {
                iNTEXDataSet = ((INTEXDataSet)(this.FindResource("iNTEXDataSet")));
                setUI = (DataSet)TryFindResource("setUI");
                if (setUI == null)
                {
                    setUI = new DataSet();
                }
                tbUI = new DataTable("TableUI");
                tbUI.Columns.Add("ART", typeof(string));
                tbUI.Columns.Add("KRS", typeof(string));
                tbUI.Columns.Add("LFD", typeof(string));
                tbUI.Columns.Add("POS", typeof(string));
                tbUI.Columns.Add("FROMORDERDATE", typeof(string));
                tbUI.Columns.Add("ENDORDERDATE", typeof(string));
                tbUI.Columns.Add("FROMINCOMEDATE", typeof(string));
                tbUI.Columns.Add("ENDINCOMEDATE", typeof(string));
                dwUI = tbUI.NewRow(); 
                this.setUI.Tables.Clear();
                /*this.setUI.Tables.Add("GSDAT");
                this.setUI.Tables.Add("GSCOLOR");
                this.setUI.Tables.Add("LIEFDAT");*/
                //DataSet setResult = m_Data.GetGSDAT();

                //setUI.Merge(setResult);
                iNTEXDataSet.Merge(setUI);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void FromOrderDate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                fromorderdate = m_Data.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void EndOrderDate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            { 
                endorderdate = m_Data.GetDate(sender); 
            } 
            catch (Exception ex)
            { 
                Tracer.Error(ex.StackTrace, ex); 
            } 
        }
        private void FromIncomeDate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            { 
                fromincomedate = m_Data.GetDate(sender); 
            } 
            catch (Exception ex)
            { 
                Tracer.Error(ex.StackTrace, ex); 
            }
        } 
        private void EndIncomeDate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                endincomedate = m_Data.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    dwUI["ART"] = txtArticle.Text.ToString().Trim();
                    dwUI["KRS"] = txtKRS.Text.ToString().Trim();
                    dwUI["LFD"] = txtLFD.Text.ToString().Trim();
                    string POS = txtPOS.Text.ToString().Trim();
                    dwUI["POS"] = m_Data.Right("000" + POS, 3);
                    dwUI["FROMORDERDATE"] = fromorderdate;
                    dwUI["ENDORDERDATE"] = endorderdate;
                    dwUI["FROMINCOMEDATE"] = fromincomedate;
                    dwUI["ENDINCOMEDATE"] = endincomedate;
                    DataSet setResult = m_Data.BuildData(dwUI);
                    setUI.Clear();
                    setUI.Merge(setResult);
                    this.MasterData.DataContext = setUI;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnReport_Click(object sender, RoutedEventArgs e)
        {
            m_DataX = new EK03Data(dicParamater);
            m_DataX.OracleDB = this.OracleDB;
            m_DataX.proxy = this.proxy;
            m_rpt.exAgent = m_DataX.exAgent;
            m_rpt.dsSource = setUI;
            m_rpt.REPORT2EXCEL("EXCEL"); 
        } 
    }
}